package utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * beanutils
 */
public class BeansUtils extends org.springframework.beans.BeanUtils {

    public static Map<String, Object> toMap(Object bean) {
        return JSON.parseObject(JSON.toJSONString(bean));
    }

    public static <T> T copyDepthProperties(Object source, Class<T> targetClass) {
        if (source == null) {
            return null;
        }
        return JSONObject.parseObject(JSONObject.toJSONString(source), targetClass);
    }


    public static <T, S> List<T> copyDepthProperties(List<S> sources, Class<T> targetClass) {
        if (sources == null) {
            return null;
        }
        return JSONObject.parseArray(JSONObject.toJSONString(sources), targetClass);
    }


    public static <T, S> List<T> copyProperties(List<S> sources, Class<T> targetClass) {
        return copyDepthProperties(sources, targetClass);
    }

    public static <T> T copyProperties(Object source, Class<T> targetClass) {
        return copyDepthProperties(source, targetClass);
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }
}
